package me.service.impl;

import me.dao.StudentDao;
import me.domain.Student;
import me.service.StudentService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 学生信息交互方法的service
 *
 * @author Rainh
 */
@Service(value = "studentService")
@Scope(value = "prototype")
public class StudentServiceImpl implements StudentService {

    //实例化StudentDao对象
    //private StudentDao studentDao = new StudentDao();
    //使用工厂创建StudentDao对象
//    private StudentDao studentDao = FactoryDaoImpl.getInstance().createStudentDao();
    //通过工厂类获取StudentDao对象  使用多例  如果需要单例需要写在方法内部
//    private StudentDao studentDao = (StudentDao)Factory.getBeans("studentDao");
    //通过spring容器获取容器对象
//    ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
    //通过容器获取studentDao对象
//    StudentDao studentDao = ac.getBean("studentDao",StudentDaoImpl.class);

    //自动注入的方式
//    @Resource(name = "studentDao")
//    private StudentDao studentDao = new StudentDaoImpl();

    @Resource(name = "studentDao")
    private StudentDao studentDao;

    /**
     * 获取所有学生信息的方法
     * @return
     */
    @Override
    public List<Student> getAllStudents(){
        //直接调用studentDao的方法就可以了
        return studentDao.queryAllStudents();
    }

    /**
     * 添加学生信息
     * @param student
     */
    @Override
    public void addStudent(Student student){
        //调用StudentDao的方法
        studentDao.addStudent(student);
    }

    /**
     * 通过ID查找学生信息
     * @param id
     * @return
     */
    @Override
    public Student findStudentById(long id){
        //调用studentDao的方法
        return studentDao.findStudentById(id);
    }

    /**
     * 修改学生信息
     * @param student
     */
    @Override
    public void updateStudent(Student student){
        //调用studentDao的方法
        studentDao.updateStudent(student);
    }

    /**
     * 通过学号删除学生信息
     * @param id
     */
    @Override
    public void deleteStudentById(long id) {
        //调用studentDao的方法
        studentDao.deleteStudentById(id);

    }
}
